let tools = {
  baseUrl:'',
  setTitle:function(text){
    document.title = text || '';
  },
  cloneObj : function (obj) {
      var str, newobj = obj.constructor === Array ? [] : {};
      if (typeof obj !== 'object') {
          return;
      } else if (JSON) {
          str = JSON.stringify(obj), //序列化对象
              newobj = JSON.parse(str); //还原

      } else {
          for (var i in obj) {
              newobj[i] = typeof obj[i] === 'object' ? cloneObj(obj[i]) : obj[i];
          }
      }
      return newobj;
  },
  setSession: function (key, val) {
    window.sessionStorage[key] = typeof val == 'string' || typeof val == 'number' ? val : JSON.stringify(val);
  },
  getSession: function (key) {

    try {
      let data = window.sessionStorage[key];
      if (data) {
        data = JSON.parse(data);
        return data;
      }
    } catch (e) {
      return '';
    }
  },
  clearSession: function () {
    window.sessionStorage.clear();
  },
  initHeader:function(appChannel,version,appType,extend){
    let obj = {
      timestamp : new Date().getTime(),
      appChannel,
      version,
      appType
    };

    if(extend){
      for(let i in extend){
        obj[i] = extend[i];
      }
    };
    
    return obj;
  },
  ajax:function(obj){
      var _this = this;

      if(!obj.type){
        obj.type = 'POST';
      }

      obj.headers = obj.headers || {};

      if(obj.type == 'POST'){
        obj.headers["Accept"] = 'application/json';
        obj.headers["Content-Type"] = 'application/json';
      }

      // try{
      //   let timestamp = new Date().getTime();//时间戳
      //   let appChannel = 'WECHAT';//平台
      //   let version = '1.0.0';//版本号
      //   let appType = obj.headerType || 'AGENT';//类型
        
      //   obj.headers['timestamp'] = timestamp;
      //   obj.headers['appChannel'] = appChannel;
      //   obj.headers['version'] = version;
      //   obj.headers['appType'] = appType;
      // }catch(e){

      // }

      if(obj.data && obj.needJson == void 0 && obj.type == 'POST'){
        obj.data = JSON.stringify(obj.data)
      }
      
      return new Promise( (resolve, reject) => {
        obj.url = obj.url.replace(/(^\/\w*_service)/,'/matchA$1');
        
        $.ajax({
          headers : obj.headers,
          type: obj.type,
          timeout : 30000, //超时时间设置，单位毫秒
          url:this.baseUrl+obj.url,
          async: true,
          dataType:obj.dataType || 'json',
          data: obj.data || {},
          success: function(res){
            let code = res.code;
            console.log(obj.url +'-----V-----');
            console.log(res);

            if(code == 200){

            }else if(code >= 300 && code <= 399){
              //登录失效 重新登录
              // throw new Error('登录失效');
            }else if(code >= 400 && code <= 499){
              //数据格式异常
              // throw new Error('数据格式异常');
            }else if(code >= 500 && code <= 599){
              //服务端异常
              // throw new Error('服务端异常');
            }else if(code >= 600){
              //未知异常
              // throw new Error('未知异常');
            }

            return resolve(res);
          },
          error: function(res){

            return resolve(res);
          },
          complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
            if(status=='timeout'){//超时,status还有success,error等值的情况
              alert('服务器超时，请稍后再试');
            }

      　　}
        });

      });

    }
};

export default tools;